package bb.lanxing.calc.calculator;

import bb.lanxing.calc.data.SourcePoint;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SPConstant;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.DistanceUtil;

public class IntervalCalc {
    private static final double MIN_STORE_DISTANCE_CYCLING = 8.0d;
    private static final int MIN_STORE_DISTANCE_RUNNING = 1;
    private static final double MIN_STORE_DISTANCE_WALKING = 1.0d;
    private static final int MIN_STORE_DURATION = 5000;
    private SourcePoint lastValidSourcePoint;

    public boolean isNeedStore(SourcePoint sourcePoint) {
        if (RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING) == Workout.TYPE_TRAINING) {
            return true;
        }
        if (RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING) == Workout.TYPE_INDOOR_BIKE) {
            return true;
        }
        if (RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING) == Workout.TYPE_BIKE_TRAINER) {
            return true;
        }
        if (RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING) == Workout.TYPE_ROWER) {
            return true;
        }
        if (RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING) == Workout.TYPE_CROSS_TRAINER) {
            return true;
        }

        return checkWithDistance(sourcePoint);
    }

    private boolean checkWithDuration(SourcePoint sourcePoint) {
        if (this.lastValidSourcePoint == null || sourcePoint.getTimestamp() - this.lastValidSourcePoint.getTimestamp() > MIN_STORE_DURATION) {
            this.lastValidSourcePoint = sourcePoint;
            return true;
        }
        return false;
    }

    private boolean checkWithDistance(SourcePoint sourcePoint) {
        SourcePoint sourcePoint2 = this.lastValidSourcePoint;
        if (sourcePoint2 == null || DistanceUtil.get(sourcePoint, sourcePoint2) > getMinStoreDistance()) {
            this.lastValidSourcePoint = sourcePoint;
            return true;
        }
        return false;
    }

    private double getMinStoreDistance() {
        int i = RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING);
        return (i == Workout.TYPE_WALKING || i == Workout.TYPE_RUNNING) ? MIN_STORE_DISTANCE_WALKING : MIN_STORE_DISTANCE_CYCLING;
    }

    public void reset() {
        this.lastValidSourcePoint = null;
    }
}
